package com.etc.sfts.controller;

import com.etc.sfts.biz.CinemaBiz;
import com.etc.sfts.dto.PageDto;
import com.etc.sfts.entity.Cinema;
import com.etc.sfts.entity.Manager;
import com.etc.sfts.entity.Message;
import com.google.gson.Gson;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@Controller
public class DoCinemaController {
    @Autowired
    private CinemaBiz biz;

    public CinemaBiz getBiz() {
        return biz;
    }

    public void setBiz(CinemaBiz biz) {
        this.biz = biz;
    }

    //获得影院总数
    @RequestMapping("/getCinemaCount")

    public void getCinemaCount(HttpServletResponse response) throws Exception {
        Long count = biz.getCinemaCount();
        PrintWriter out = response.getWriter();
        out.print(count);
        out.flush();
        out.close();
    }
//获取所有影院
    @RequestMapping("/getAllCinema")
    public void getAllCinema(HttpServletResponse response, HttpServletRequest request) throws  Exception{
        Integer page = 1;
        Integer size = 5;

        try {

            if (request.getParameter("page") != null) {

                page = Integer.valueOf(request.getParameter("page"));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        try {

            if (request.getParameter("size") != null) {

                size = Integer.valueOf(request.getParameter("size"));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

        size = size <= 0 ? 5 : size;
        page = page <= 0 ? 1 : page;

        Long count = biz.getCinemaCount();
        System.out.println(count);

        Integer maxPage = (int) (count % size == 0 ? (count / size) : (count / size + 1));

        page = page > maxPage ? maxPage : page;
        List<Cinema> list = biz.getAllCinema((page-1)*size, size);
        System.out.println(list);

        PageDto dto = new PageDto();

        dto.setMaxPage(maxPage);
        dto.setOlist(list);
        dto.setPage(page);
        dto.setSize(size);



        Message m = new Message();
        m.setFlag(1);
        m.setMsgcontent(dto);

        Gson json = new Gson();
        String str = json.toJson(m);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        out.print(str);
        out.flush();
        out.close();

    }
    //删除影院
    @RequestMapping("/deleteCinemaByMovieId")
    public  void deleteCinemaByMovieId(HttpServletRequest request,HttpServletResponse response)throws Exception{
        String pid=request.getParameter("cinemaid");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        try {
            Integer id=Integer.valueOf(pid);

            if(biz.deleteCinemaById(id)) {
                out.print("<script>alert('删除成功！');location.href='cinemaList.jsp';</script>");
            }else {
                out.print("<script>alert('删除失败！');histroy.go(-1);</script>");
            }
        } catch (Exception e) {
            out.print("<script>alert('参数异常，删除失败！');histroy.go(-1);</script>");
        }
        out.flush();
        out.close();

    }
    //添加影院
    @RequestMapping("/addCinema")
    public  void addCinema(HttpServletResponse response,HttpServletRequest request)throws Exception{
        // 获取登录的管理员
        // 获取session中保存的管理员
        Manager manager = (Manager) request.getSession().getAttribute("Manager");
        String cname = request.getParameter("name");
        String carea = request.getParameter("area");
        String caddress = request.getParameter("address");
        String cphone = request.getParameter("phone");
        String cdistance = request.getParameter("distance");
        String cprice = request.getParameter("price");
        if (manager == null) {
            // 设置响应类型和编码
            response.setContentType("text/html;charset=utf-8");
            // 获取指定的输出对象
            PrintWriter pw = response.getWriter();
            pw.print("<script>alert('会话失效，请先登录');location.href='login.jsp';</script>");
            pw.flush();
            pw.close();
        } else {

            // 封装要插入的影院对象
            Cinema cinema = new Cinema();
            cinema.setCinema_name(cname);
            cinema.setCinema_area(carea);
            cinema.setCinema_address(caddress);
            cinema.setCinema_phone(cphone);
            cinema.setManager_id(manager.getManager_id());
            cinema.setCinema_distance(Double.parseDouble(cdistance));
            cinema.setCinema_price(Double.parseDouble(cprice));

            // 设置响应类型和编码
            response.setContentType("text/html;charset=utf-8");
            // 获取指定的输出对象
            PrintWriter out = response.getWriter();

            // 调用业务逻辑层的添加影院方法
            if (biz.insertCinema(cinema)) {
                // 添加成功
                out.print("<script>alert('影院添加成功');location.href='cinemaList.jsp'</script>");
            } else {
                // 添加失败
                out.print("<script>alert('影院添加失败');history.go(-1);</script>");
            }
            out.flush();
            out.close();
        }

    }
    //导出excel文件
    @RequestMapping("/exporCinematExcel")
    @ResponseBody
    public String exporCinematExcel(HttpServletResponse response, Map<String, Object> params) {

        HSSFWorkbook workbook = biz.exportExcel(params);
        try {
            if (response != null) {
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                response.setHeader("Content-Disposition",
                        "attachment;filename=\"" + new String(("影院信息" + ".xls").getBytes("gb2312"), "ISO8859-1"));
                OutputStream out = response.getOutputStream();
                workbook.write(out);
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
